﻿#pragma once
#include <iostream>
//#include <utility>
using namespace std;
enum Colour {
	RED,
	BLACK
};

//BRTree树结构
template<class K, class V>
struct RBTreeNode
{
	// 这⾥更新控制平衡也要加⼊parent指针
	pair<K, V> _kv;
	RBTreeNode<K, V>* _left;
	RBTreeNode<K, V>* _right;
	RBTreeNode<K, V>* _parent;
	Colour _col;
	RBTreeNode(const pair<K, V>& kv = { K(),V() })
		: _kv(kv)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
	{}
};

// 请模拟实现红黑树的插入--注意：为了后序封装map和set，本文在实现时给红黑树多增加了一个头结点
template<class k, class v>
class RBTree
{
	typedef RBTreeNode<k, v> Node;
public:
	RBTree()
	{
		_pHead = new Node;
		_pHead->_left = nullptr;
		_pHead->_right = nullptr;
	}

	// 在红黑树中插入值为data的节点，插入成功返回true，否则返回false
	// 注意：为了简单起见，本次实现红黑树不存储重复性元素
	bool Insert(const pair<k, v>& V)
	{
		if (_pHead->_kv.first == k() && _pHead->_kv.second == v())
		{
			_pHead->_kv = V;
			_pHead->_col = BLACK;
			return true;
		}
		Node* parent = nullptr;
		Node* cur = _pHead;
		while (cur)
		{
			if (cur->_kv.first < V.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_kv.first > V.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
			if (cur == _pHead)
			{
				break;
			}
		}
		cur = new Node(V);
		cur->_col = RED;
		if (parent->_kv.first < V.first)
		{
			parent->_right = cur;
		}
		else
		{
			parent->_left = cur;
		}
		cur->_parent = parent;
		//对RBTree进行调整
		Node* u = nullptr;
		//Node* g = parent->_parent;
		while (parent && parent->_col == RED)
		{
			Node* g = parent->_parent;
			if (g->_left == parent)
			{
				u = g->_right;
				//1.单变色
				if (u && u->_col == RED)
				{
					u->_col = BLACK;
					parent->_col = BLACK;
					g->_col = RED;

					cur = g;
					parent = cur->_parent;
					//g = parent->_parent;
				}
				else
				{
					//2.变色+单旋
					// u 在 g 的右进行有单旋
					if (cur == parent->_left)
					{
						/*g->_left = parent->_right;
						if (parent->_right)
							parent->_right->_parent = g;
						parent->_right = g;
						parent->_parent = g->_parent;
						g->_parent = parent;*/
						RotateR(g);
						parent->_col = BLACK;
						g->_col = RED;
					}
					//3.双转
					else
					{
						RotateL(parent);
						RotateR(g);
						cur->_col = BLACK;
						g->_col = RED;
					}
					break;
				}
			}
			else if (g->_right == parent)
			{
				u = g->_left;
				if (u && u->_col == RED)
				{
					u->_col = BLACK;
					parent->_col = BLACK;
					g->_col = RED;
					cur = g;
					parent = cur->_parent;
					//g = parent->_parent;
				}
				else
				{
					if (cur == parent->_right)
					{
						RotateL(g);
						parent->_col = BLACK;
						g->_col = RED;
					}
					else
					{
						RotateR(parent);
						RotateL(g);
						cur->_col = BLACK;
						g->_col = RED;
					}
					break;
				}
			}
			_pHead->_col = BLACK;
		}
		return true;
	};
	// 检测红黑树中是否存在值为data的节点，存在返回该节点的地址，否则返回nullptr
	Node* Find(const k& key)
	{
		Node* data = _pHead;
		while (data)
		{
			if (data->_kv.first == key)
				return _pHead;
			if (data->_kv.first < key)
			{
				data = data->_right;
			}
			else if (data->_kv.first > key)
			{
				data = data->_left;
			}
		}
		return nullptr;
	}

	// 获取红黑树最左侧节点
	Node* LeftMost()
	{
		return _leftMost(_pHead);
	}

	// 获取红黑树最右侧节点
	Node* RightMost()
	{
		Node* x = _pHead;
		while (x && x->_right != nullptr)
		{
			x = x->_right;
		}
		return x;
	}
	void InOrder()
	{
		_InOrder(_pHead);
		cout << endl;
	}

	int Height()
	{
		return _Height(_pHead);
	}

	int Size()
	{
		return _Size(_pHead);
	}

	// 检测红黑树是否为有效的红黑树，注意：其内部主要依靠_IsValidRBTRee函数检测
	bool IsValidRBTRee()
	{
		if (_pHead == nullptr)
			return true;
		else if (_pHead->_col == RED)
			return false;

		int refnum = 0;
		Node* cur = _pHead;
		while (cur)
		{
			if (cur->_col == BLACK)
			{
				refnum++;
			}
			cur = cur->_left;
		}
		return _IsValidRBTRee(_pHead, 0, refnum);
	}

private:
	bool _IsValidRBTRee(Node* pRoot, size_t blackCount, size_t pathBlack)
	{
		if (pRoot == nullptr)
		{
			if (blackCount == pathBlack)
				return true;
			else
			{
				cout << "存在黑色结点的数量不相等的路径" << endl;
				return false;
			}
		}
		if (pRoot->_col == RED && pRoot->_parent->_col == RED)
		{
			cout << pRoot->_kv.first << "存在连续的红色结点" << endl;
			return false;
		}
		if (pRoot->_col == BLACK)
		{
			blackCount++;
		}
		return _IsValidRBTRee(pRoot->_left, blackCount, pathBlack) && _IsValidRBTRee(pRoot->_right, blackCount, pathBlack);
	}

	void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}

		_InOrder(root->_left);
		cout << root->_kv.first << ":" << root->_kv.second << endl;
		_InOrder(root->_right);
	}

	int _Height(Node* root)
	{
		if (root == nullptr)
			return 0;
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
	}

	int _Size(Node* root)
	{
		if (root == nullptr)
			return 0;

		return _Size(root->_left) + _Size(root->_right) + 1;
	}
	// 左单旋
	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;

		Node* parentParent = parent->_parent;
		subR->_left = parent;
		parent->_parent = subR;
		if (parentParent == nullptr)
		{
			_pHead = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (parent == parentParent->_left)
			{
				parentParent->_left = subR;
			}
			else
			{
				parentParent->_right = subR;
			}
			subR->_parent = parentParent;
		}
	}
	// 右单旋
	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		parent->_left = subLR;
		if (subLR)
			subLR->_parent = parent;

		Node* pParent = parent->_parent;

		subL->_right = parent;
		parent->_parent = subL;

		if (parent == _pHead)
		{
			_pHead = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (pParent->_left == parent)
			{
				pParent->_left = subL;
			}
			else
			{
				pParent->_right = subL;
			}

			subL->_parent = pParent;
		}
	}
	// 为了操作树简单起见：获取根节点
	Node*& GetRoot()
	{
		return _pHead;
	}

	Node* _leftMost(Node* node)
	{
		if (node == nullptr || node->_left == nullptr)
		{
			return node;
		}
		_leftMost(node->_left);
	}
	private:
		Node* _pHead;
};
